package com.thrd.taiexhibition.fragement;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.flyco.tablayout.SlidingScaleTabLayout;
import com.thrd.basicframework.bean.BaseFragment;
import com.thrd.basicframework.ui.XViewPager;
import com.thrd.basicframework.ui.defaultview.LoadingState;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.adapter.ThemeAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 观城市
 * @author: ${唐杰}
 * @date: 2021/3/5
 */
public class CityFragment extends BaseFragment {
    private static final String TAG="CityFragment";
    private String[] mTitlesArrays ={"首页","城市故事","重点产业","投资环境","热点关注"};
    private XViewPager mViewPager;
    private SlidingScaleTabLayout mSlidingScaleTabLayout;
    private CityHomeFragment fragment1;
    private CityFocusFragment fragment3;
    private CityInvestmentEnvironmentFragment fragment4;
    private CityStoryFragment fragment2;
    private CityHotSpotFragment fragment5;


    @Override
    protected int setLayoutResourceID() {
        return R.layout.city_fragment;
    }

    @Override
    protected void setUpView(View mContentView) {
        mSlidingScaleTabLayout = mContentView.findViewById(R.id.mSlidingTabLayout);
        mViewPager = mContentView.findViewById(R.id.vp_pager);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setSlide(true);
    }

    @Override
    protected void setUpData() {
        getContentView().setState(LoadingState.STATE_SUCCESS);
        fragment1 = new CityHomeFragment();
        fragment2 = new CityStoryFragment();
        fragment3 = new CityFocusFragment();
        fragment4 = new CityInvestmentEnvironmentFragment();
        fragment5 = new CityHotSpotFragment();
        List<BaseFragment> fragmentList=new ArrayList<>();
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
        fragmentList.add(fragment5);
        mViewPager.setOffscreenPageLimit(1);
        ThemeAdapter adapter=new ThemeAdapter(getChildFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,fragmentList);
        mViewPager.setAdapter(adapter);
        mSlidingScaleTabLayout.setViewPager(mViewPager, mTitlesArrays);//tab和ViewPager进行关联

//        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });

    }
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {

        Log.e(TAG,"onSaveInstanceState");
//        outState.putSerializable("BeanCityIndustryList", mBeanCityIndustryList);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e("CityFragment","onDestroyView");
    }

    @Override
    public void onFragmentResume() {
        super.onFragmentResume();
        if(fragment1!=null){
            fragment1.onFragmentResume();
        }
    }

    @Override
    public void onFragmentPause() {
        super.onFragmentPause();
        if(fragment1!=null){
            fragment1.onFragmentPause();
        }
    }


    public void jumpColumn(int  column){
        switch (column){
            case 4:
                if(mViewPager!=null)mViewPager.setCurrentItem(1);
                break;
            case 5:
                if(mViewPager!=null)mViewPager.setCurrentItem(2);
                break;
            case 6:
                if(mViewPager!=null)mViewPager.setCurrentItem(3);
                break;
            case 7:
                if(mViewPager!=null)mViewPager.setCurrentItem(4);
                break;
        }
    }
}
